package com.ym.web;

import com.ym.domain.Build;
import com.ym.domain.Device;
import com.ym.domain.Floor;
import com.ym.dto.FloorDto;
import com.ym.fegin.DeviceFeignClient;
import com.ym.service.IBuildService;
import com.ym.util.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/build")//localhost:8080/api/build/add
public class BuildController {

    @Autowired
    private IBuildService iBuildService;

    @Autowired
    private DeviceFeignClient deviceFeignClient;

    @PostMapping("/addFloor")//localhost:8080/api/build/addFloor
    public JSONResult addFloor(@RequestBody List<FloorDto> list){
        //return floorFeignClient.addFloor(list);
        return iBuildService.addFloor(list);
    }

    @GetMapping("/getDeviceListByBuildId")
    public JSONResult getDeviceListByBuildId(@RequestParam Long id){
        Build build = iBuildService.findByid(id);
        List<Floor> floorList = iBuildService.getFloorListByBuildId(id);
        for (Floor floor : floorList) {
            System.out.println(floor);
            System.out.println(id+":"+floor.getFloorId());
            List<Device> devices = deviceFeignClient.getDeviceList(id,floor.getFloorId());
            floor.setDeviceList(devices);
        }
        build.setFloorList(floorList);
        return JSONResult.success(build);
    }

    @GetMapping("/getDeviceListByDeviceType")
    public JSONResult getDeviceListByDeviceType(@RequestParam Integer type){
        ArrayList<Object> list = new ArrayList<>();
        List<Device> deviceList = deviceFeignClient.getDeviceListByDeviceType(type);
        for (Device device : deviceList) {
            Build build = iBuildService.findByid(device.getBuildId());
            List<Floor> floorList = iBuildService.getFloorListByBuildId(build.getId());
            for (Floor floor : floorList) {
                List<Device> devices = deviceFeignClient.getDeviceList(build.getId(),floor.getFloorId());
                floor.setDeviceList(devices);
            }
            build.setFloorList(floorList);
            list.add(build);
        }
        return JSONResult.success(list);
    }
}
